package app.util.image.action.morph
{
	import app.util.*;
	import app.util.image.*;
	import app.util.image.action.*;
	import app.util.image.calc.*;
	
	import flash.display.*;

	public class BinaryReconstructionAction extends SEBasedAction
	{
		private var progress:Number = 0;
		private var o:BinaryBitmapData;
		private var d:BinaryBitmapData;
		private var src:BinaryBitmapData;

		public override function getDescription():String
		{
			return "Binary Reconstruction";
		}
		
		protected override function getFinalBitmapData():BitmapData
		{
			return this.d.toBitmapData();
		}
		
		protected override function processBitmapDataByStep():Number
		{
			if(this.progress == 0)
			{
				this.src = MorphUtility.autoThreshold(Mediator.getCurrentBitmapData());
				this.o = MorphUtility.doErosion(src, se);
				this.progress = 0.15;
			}
			else if(this.progress == 0.15)
			{
				this.o = MorphUtility.doDilation(o, se);
				this.progress = 0.3;
			}
			else
			{
				this.d = MorphUtility.doDilation(o, se);
				this.d = this.d.intersect(src);
				if(!this.d.equal(this.o))
				{
					this.o = this.d;
					this.progress += 0.05;
					if(this.progress > 0.9) this.progress = 0.92; 
				}
				else
				{
					this.progress = 1.0;
				}	
			}
			
			return this.progress;
		}
	}
}